package com.guanyong.webapi.controllers.api;

import com.guanyong.model.CommentGoodLogModel;
import com.guanyong.model.CommentModel;
import com.guanyong.model.CommentReportLogModel;
import com.guanyong.model.UserBaseModel;
import com.guanyong.services.CommentService;
import com.guanyong.utils.Comm.ResultCode;
import com.guanyong.utils.Comm.ResultObject;
import com.guanyong.webapi.handle.UserHandle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * Created by mxk on 2017/6/15.
 */
@RestController
@RequestMapping("/api/comment/")
public class CommentApi {
    @Autowired
    CommentService commentService;

    @Autowired
    UserHandle userHandle;

    //获取某部电影的评论  以及回复
    @RequestMapping(value = "getcommentlist", method = RequestMethod.GET)
    ResultObject<HashMap<String,Object>> getcommentlist(HttpServletRequest request,
                                                        @RequestParam Integer classtype,
                                                        @RequestParam String  objectid,
                                                        @RequestParam Integer parentid,
                                                        @RequestParam int pno,
                                                        @RequestParam int pagesize,
                                                        @RequestParam int allcount) {
        ResultObject<HashMap<String,Object>> result = new ResultObject<HashMap<String,Object>>();
        CommentModel commentModel =new CommentModel();
        commentModel.setfShow(0);//显示未删除的
        commentModel.setfObjectid(objectid);
        commentModel.setfClasstype(classtype);
        commentModel.setfParentid(parentid);
        HashMap<String,Object> hashMap=commentService.GetCommentList(commentModel,
                pno,
                pagesize,
                allcount);
        result.setResult(hashMap);
        return result;
    }


    //获取我的评论
    @RequestMapping(value = "getmycommentlist", method = RequestMethod.GET)
    ResultObject<HashMap<String,Object>> getmycommentlist(HttpServletRequest request,
                                                        @RequestParam int pno,
                                                        @RequestParam int pagesize,
                                                        @RequestParam int allcount) {
        ResultObject<HashMap<String,Object>> result = new ResultObject<HashMap<String,Object>>();

        UserBaseModel userBaseModel=userHandle.getCurUser(request);
        if (userBaseModel!=null && !userBaseModel.getfId().equals("")) {
            CommentModel commentModel = new CommentModel();
            commentModel.setfShow(0);//显示未删除的
            commentModel.setfCommentid(userBaseModel.getfId());
            commentModel.setfParentid(0);
            HashMap<String, Object> hashMap = commentService.GetCommentList(commentModel,
                    pno,
                    pagesize,
                    allcount);
            result.setResult(hashMap);
        }
        else{
            result.setCode(ResultCode.NoLogin);
        }
        return result;
    }

    //获取我的评论
    @RequestMapping(value = "getmycommentlist1_0", method = RequestMethod.GET)
    ResultObject<HashMap<String,Object>> getmycommentlist1_0(HttpServletRequest request,
                                                          @RequestParam int pno,
                                                          @RequestParam int pagesize,
                                                          @RequestParam int allcount) {
        ResultObject<HashMap<String,Object>> result = new ResultObject<HashMap<String,Object>>();

        UserBaseModel userBaseModel=userHandle.getCurUser(request);
        if (userBaseModel!=null && !userBaseModel.getfId().equals("")) {
            CommentModel commentModel = new CommentModel();
            commentModel.setfShow(0);//显示未删除的
            commentModel.setfCommentid(userBaseModel.getfId());
            commentModel.setfParentid(0);
            HashMap<String, Object> hashMap = commentService.GetCommentList1_0(commentModel,
                    pno,
                    pagesize,
                    allcount);
            result.setResult(hashMap);
        }
        else{
            result.setCode(ResultCode.NoLogin);
        }
        return result;
    }


    //获取我的回复
    @RequestMapping(value = "getmyanswerlist", method = RequestMethod.GET)
    ResultObject<HashMap<String,Object>> getmyanswerlist(HttpServletRequest request,
                                                          @RequestParam int pno,
                                                          @RequestParam int pagesize,
                                                          @RequestParam int allcount) {
        ResultObject<HashMap<String,Object>> result = new ResultObject<HashMap<String,Object>>();

        UserBaseModel userBaseModel=userHandle.getCurUser(request);
        if (userBaseModel!=null && !userBaseModel.getfId().equals("")) {
            CommentModel commentModel = new CommentModel();
            commentModel.setfShow(0);//显示未删除的
            commentModel.setfTocommentid(userBaseModel.getfId());
            HashMap<String, Object> hashMap = commentService.GetCommentList(commentModel,
                    pno,
                    pagesize,
                    allcount);
            result.setResult(hashMap);
        }
        else{
            result.setCode(ResultCode.NoLogin);
        }
        return result;
    }
    //登录验证
    @RequestMapping(value = "addcomment", method = RequestMethod.POST)
    ResultObject<Integer> AddComment(HttpServletRequest request,
                                                        @RequestParam Integer classtype,
                                                        @RequestParam String objectid,
                                                        @RequestParam Integer parentid,
                                                        @RequestParam String  content,
                                                        @RequestParam String tocommentid) {
        ResultObject<Integer> result = new ResultObject<Integer>();
        UserBaseModel userBaseModel=userHandle.getCurUser(request);
        if (userBaseModel!=null && !userBaseModel.getfId().equals("")) {
            CommentModel commentModel = new CommentModel();
            commentModel.setfClasstype(classtype);
            commentModel.setfObjectid(objectid);
            commentModel.setfCommentid(userBaseModel.getfId());
            commentModel.setfTocommentid(tocommentid);
            commentModel.setfParentid(parentid);
            commentModel.setfContent(content);
            result = commentService.AddComment(commentModel);


        }
        else{
            result.setCode(ResultCode.NoLogin);
        }
        return result;
    }


    //登录验证
    @RequestMapping(value = "delcomment", method = RequestMethod.POST)
    ResultObject<Integer> DelComment(HttpServletRequest request,
                                     @RequestParam Integer commentid
                                 ) {
        ResultObject<Integer> result = new ResultObject<Integer>();
        UserBaseModel userBaseModel=userHandle.getCurUser(request);
        if (userBaseModel!=null && !userBaseModel.getfId().equals("")) {
            Integer res = commentService.UpdatePraentIdSate(commentid, 1);
            if (res > -1) {
                result.setCode(ResultCode.OperateOk);
            } else {
                result.setCode(ResultCode.OperateFail);
            }
        }
        else{
            result.setCode(ResultCode.NoLogin);
        }
        return result;
    }

    //登录验证
    @RequestMapping(value = "addcommentgoodlog", method = RequestMethod.POST)
    ResultObject<Integer> AddCommentGoodLog(HttpServletRequest request,
                                            @RequestParam Integer commentid,
                                            @RequestParam Integer apptype) {
        ResultObject<Integer> result = new ResultObject<Integer>();
        UserBaseModel userBaseModel=userHandle.getCurUser(request);
        if (userBaseModel!=null && !userBaseModel.getfId().equals("")) {

            

            String ip = com.guanyong.utils.NetUtils.IPUtil.getIpAddr(request);
            CommentGoodLogModel commentGoodLogModel = new CommentGoodLogModel();
            commentGoodLogModel.setfUserid(userBaseModel.getfId());
            commentGoodLogModel.setfApptype(apptype);
            commentGoodLogModel.setfIp(ip);
            commentGoodLogModel.setfCommentid(commentid);
            result= commentService.AddComentGoodLog(commentGoodLogModel);
        }
        else{
            result.setCode(ResultCode.NoLogin);
        }
        return  result;

    }


    //登录验证
    @RequestMapping(value = "addcommentreprotlog", method = RequestMethod.POST)
    ResultObject<Integer> AddCommentReprotLog(HttpServletRequest request,
                                     @RequestParam Integer commentid,
                                     @RequestParam String  content,
                                     @RequestParam Integer apptype) {
        ResultObject<Integer> result = new ResultObject<Integer>();

        UserBaseModel userBaseModel=userHandle.getCurUser(request);
        if (userBaseModel!=null && !userBaseModel.getfId().equals("")){
            String ip=com.guanyong.utils.NetUtils.IPUtil.getIpAddr(request);
            CommentReportLogModel commentReportLogModel=new CommentReportLogModel();
            commentReportLogModel.setfUserid(userBaseModel.getfId());
            commentReportLogModel.setfApptype(apptype);
            commentReportLogModel.setfIp(ip);
            commentReportLogModel.setfCommentid(commentid);
            commentReportLogModel.setfReportcontent(content);
            result= commentService.AddComentReprotLog(commentReportLogModel);
        }
        else{
            result.setCode(ResultCode.NoLogin);
        }


        return result;
    }



}
